package com.xm.xmapp.customview;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.xm.xmapp.R;

public class DrawCanvas extends View {

	float startAngle01 = 0;
	float sweepAngle01 = 360;
	int circleWith;

	public DrawCanvas(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint.setStyle(Paint.Style.STROKE);
		circleWith = (int) getResources().getDimension(R.dimen.dp10);
		paint.setStrokeWidth(circleWith);
		color = ContextCompat.getColor(context, R.color.startcolor);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int with = (int) getResources().getDimension(R.dimen.dp71);
		int height = (int) getResources().getDimension(R.dimen.dp71);
		setMeasuredDimension(with, height);
	}

	int color;
	Paint paint = new Paint();

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setColor(color);
		RectF rect = new RectF(circleWith / 2, circleWith / 2, getWidth() - circleWith / 2, getHeight() - circleWith
				/ 2);
		canvas.drawArc(rect, startAngle01, sweepAngle01, false, paint);
	}

	public void startAnimator1() {
		Integer colorFrom = ContextCompat.getColor(getContext(), R.color.endcolor);
		Integer colorTo = ContextCompat.getColor(getContext(), R.color.startcolor);
		ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
		colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animator) {
				color = ((Integer) animator.getAnimatedValue());
				invalidate();
			}
		});
		colorAnimation.setDuration(5000);
		colorAnimation.start();
	}

}
